﻿using AutoMapper;
using CoreProject.DTO;
using CoreProject.IService;
using CoreProject.Model;
using Microsoft.AspNetCore.Mvc;
using StackExchange.Redis;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace CoreProject.Api.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class GoodsController : ControllerBase
    {
        // GET: api/<GoodsController>
        private readonly IMapper _mapper;
        private readonly IGoodsService _service;
        private readonly IGoodsTypeService _typeService;

        public GoodsController(IMapper mapper, IGoodsService service,IGoodsTypeService typeService)
        {
            _mapper = mapper;
            _service = service;
            this._typeService = typeService;
        }
        [HttpGet]
        public async Task<ResultData<List<UniAppGoodJsonDto>>> GetGoods()
        {
            var list = from t in await _typeService.GetGoodsTypeAsync()
                       select new UniAppGoodJsonDto
                       {
                           id = t.GtId,
                           name = t.Name,
                           icon = t.Icon,
                           is_show_backstage = t.IsShowBackstage,
                           sort = t.Sort,
                           goods_list = from g in t.Goods
                                        select new UniAppGoodDto
                                        {
                                            id = g.GId,
                                            name = g.Name,
                                            content = g.Content,
                                            images = g.Images,
                                            use_spec = g.UseSpec,
                                            use_property =g.UseProperty,
                                            unit = g.Unit,
                                            price = g.Price,
                                            sales = g.Sales,
                                            sort = g.Sort,
                                            On_sole = g.OnSale,
                                            property = from p in g.GoodsPropertyKeys
                                                       select new UniAppGoodPropertyKeyDto
                                                       {
                                                           id =p.GpkId,
                                                           name = p.Gsk.Name,
                                                           values = from v in p.GoodsPropertyValues
                                                                    select new UniAppGoodPropertyValueDto
                                                                    {
                                                                        id = v.GpvId,
                                                                        is_default = v.IsDefault,
                                                                        code = v.Gsv.Code,
                                                                        value = v.Gsv.Value
                                                                    }
                                                       }

                                        }
                       };


            ResultData<List<UniAppGoodJsonDto>> result = new ResultData<List<UniAppGoodJsonDto>>();
            result.code = "1";
            result.msg = "ok";
            result.state = 200;
            result.data =list.ToList() ;
            return result;
        }

        [HttpGet("page")]
        public async Task<ResultPageList<GoodDto>> GetGoodByPage(int page,int size,string name="")
        {
            var data = await _service.GetGoodsBypage(name, page, size);
            var list = from g in data.list
                       select new GoodDto
                       {
                           GID = g.GId,
                           Name = g.Name,
                           Content = g.Content,
                           Images = g.Images,
                           Sales = g.Sales,
                           Unit = g.Unit,
                           Use_Spec = g.UseSpec,
                           Use_Property = g.UseProperty,
                           GTID = g.GtId,
                           TypeName = g.Gt.Name,
                           Price = g.Price,
                           On_sale = g.OnSale,
                           GoodPropertyKey = from gpk in g.GoodsPropertyKeys
                                                 //商品的属性 温度 糖度 配料
                                             select new GoodPropertyKeyDto
                                             {
                                                 GPKID = gpk.GpkId,
                                                 Name = gpk.Gsk.Name,
                                                 GSKID = gpk.Gskid,
                                                 GoodPropertyValue = from gpv in gpk.GoodsPropertyValues
                                                                         //属性值 
                                                                     select new GoodPropertyValueDto
                                                                     {
                                                                         GPVID = gpv.GpvId,
                                                                         GSVID = gpv.GsvId,
                                                                         Code = gpv.Gsv.Code,
                                                                         Value = gpv.Gsv.Value,
                                                                         Is_Default = gpv.IsDefault
                                                                     }
                                             }
                       };

            ResultPageList<GoodDto> result = new ResultPageList<GoodDto>();
            result.state = 200;
            result.code = "1";
            result.msg = "ok";
            result.page = page;
            result.size = size;
            result.count = data.count;
            result.data = list.ToList();
            return result;
        }

        [HttpPost]
        public async Task<ResultHandle> Post(GoodEditDto value)
        {
            Good good = new Good();
            good.GId = 0;
            good.Name = value.Name;
            good.Price = value.Price;
            good.Unit = value.Unit;
            good.GtId = value.GTID;
            good.Content = value.Content;
            good.Images = value.Images;
            good.UseSpec = value.Use_Spec;
            good.UseProperty = value.Use_Property;
            good.Sales = 0;
            good.OnSale = true;
            good.Sort = value.Sort;
            good.GoodsPropertyKeys = new List<GoodsPropertyKey>() { };
            foreach (GoodEditPropertyKeyDto gpk in value.GoodsPropertyKey)
            {
                GoodsPropertyKey key = new GoodsPropertyKey()
                {
                    GpkId = 0,
                    Gskid = gpk.GSKID,
                    GoodsPropertyValues = new List<GoodsPropertyValue>() { }
                };
                good.GoodsPropertyKeys.Add(key);
                int i = 0;
                foreach (int gsvid in gpk.GoodsPropertyValue)
                {
                    key.GoodsPropertyValues.Add(new GoodsPropertyValue()
                    {
                        GpvId = 0,
                        GsvId = gsvid,
                        IsDefault = (i == 0)

                    });
                    i++;

                }

            }

            var bol = await _service.AddAsync(good);
            ResultHandle result = new ResultHandle();
            result.state = 200;
            result.code = bol ? "1" : "-1";
            result.msg = bol ? "添加成功!" : "添加失败!";
            return result;
        }

        // PUT api/<UsersController>/5
        [HttpPut]
        public async Task<ResultHandle> Put(GoodEditDto value)
        {

            var good = new Good();
            good.GId = value.GID;
            good.Name = value.Name;
            good.Price = value.Price;
            good.Unit = value.Unit;
            good.GtId = value.GTID;
            good.UseProperty = value.Use_Property;
            good.UseSpec = false;
            good.Content = value.Content;
            good.Sales = value.Sales;
            good.OnSale = value.On_sale;
            good.Images = value.Images;
            good.Sort = value.Sort;
            good.GoodsPropertyKeys = new List<GoodsPropertyKey>() { };
            foreach (GoodEditPropertyKeyDto gpk in value.GoodsPropertyKey)
            {
                GoodsPropertyKey key = new GoodsPropertyKey()
                {
                    GpkId = 0,
                    GId = good.GId,
                    Gskid = gpk.GSKID,
                    GoodsPropertyValues = new List<GoodsPropertyValue>()

                };
                good.GoodsPropertyKeys.Add(key);
                int i = 0;
                foreach (int gsvid in gpk.GoodsPropertyValue)
                {
                    key.GoodsPropertyValues.Add(new GoodsPropertyValue()
                    {
                        GpvId = 0,
                        GsvId = gsvid,
                        IsDefault = (i == 0)
                    });
                    i++;



                }

            }


            var bol = await _service.UpdateGood(good);
            ResultHandle result = new ResultHandle();
            result.state = 200;
            result.code = bol ? "1" : "-1";
            result.msg = bol ? "修改成功!" : "修改失败!";
            return result;
        }

        // DELETE api/<UsersController>/5
        [HttpDelete]
        public async Task<ResultHandle> Delete(int id)
        {
            //Good good = new Good();
            //good.GId = id;

            var bol = await _service.DeleteGood(id);
            ResultHandle result = new ResultHandle();
            result.state = 200;
            result.code = bol ? "1" : "-1";
            result.msg = bol ? "删除成功!" : "删除失败!";
            return result;
        }

        // DELETE api/<UsersController>/5
        [HttpDelete("DeleteBatch")]
        public async Task<ResultHandle> DeleteBatch([FromBody] int[] ids)
        {
            //Good[] goods = new Good[ids.Length];
            //for (int i = 0; i < ids.Length; i++)
            //{
            //    goods[i] = new Good() { GId = ids[i] };
            //}

            var bol = await _service.DeleteGoods(ids);
            ResultHandle result = new ResultHandle();
            result.state = 200;
            result.code = bol ? "1" : "-1";
            result.msg = bol ? "删除成功!" : "删除失败!";
            return result;
        }

        [HttpPut("Shangjia")]
        public async Task<ResultHandle> PutShangjia([FromBody] IdDto gid)
        {
            var bol = await _service.ShangJia(gid.Id);
            ResultHandle result = new ResultHandle();
            result.state = 200;
            result.code = bol ? "1" : "-1";
            result.msg = bol ? "上架成功!" : "上架失败!";
            return result;

        }
        [HttpPut("Xiajia")]
        public async Task<ResultHandle> PutXiajia([FromBody] IdDto gid)
        {
            var bol = await _service.XiaJia(gid.Id);
            ResultHandle result = new ResultHandle();
            result.state = 200;
            result.code = bol ? "1" : "-1";
            result.msg = bol ? "下架成功!" : "下架失败!";
            return result;

        }
    }

    }

